package com.sxbbc.common.basics.sms;

import org.apache.commons.lang3.StringUtils;

import com.function.common.GetRest;
import com.function.common.RestResponse;
import com.sxbbc.common.basics.enums.RandNumType;
import com.sxbbc.common.basics.service.SpringBeanFactoryUtils;
import com.sxbbc.common.basics.service.redis.RedisUtilsService;
import com.sxbbc.common.basics.utils.radoms.RandNumUtils;
import com.sxbbc.common.basics.utils.regular.RpxUtils;

/**
 * 发送短信
 *
 * @author arno
 */
public class SendSms {

    public static SendSms sendSms = null;

    public static SendSms getInstance() {
        if (sendSms == null) {
            synchronized (SendSms.class) {
                if (sendSms == null) {
                    sendSms = new SendSms();
                }
            }
        }
        return sendSms;
    }

    /**
     * 聚合发送短信
     *
     * @param phone
     * @return
     */
    public RestResponse sendSmsJvhe(String phone) {
        String validPhone = RpxUtils.getInstance().validPhone(phone);
        if (!StringUtils.isBlank(validPhone)) {
            return GetRest.getFail(validPhone);
        }
        String validCode = RandNumUtils.getInstance().get(RandNumType.NUMBER, 6);
        //防止重复点击发送验证码
        String sendFlag = "sendflag_sms_" + phone;
        boolean flag = getRedisUtilsService().setIncrSecond(sendFlag, 60);
        if (!flag) {
            return GetRest.getFail("您已发送验证码,请1分钟后重试");
        }
        RestResponse result = SmsUtils.getInstance().mobileQuery(phone, validCode);
        if (result.isStatus()) {
            //保存验证码到缓存
            String key = "send_sms_" + phone;
            getRedisUtilsService().setKey(key, validCode, 600);
        }
        return result;
    }

    /**
     * kewail发送短信
     *
     * @param phone
     * @return
     */
    public RestResponse sendSmsKewail(String phone) {
        String validPhone = RpxUtils.getInstance().validPhone(phone);
        if (!StringUtils.isBlank(validPhone)) {
            return GetRest.getFail(validPhone);
        }
        String validCode = RandNumUtils.getInstance().get(RandNumType.NUMBER, 6);
        //防止重复点击发送验证码
        String sendFlag = "sendflag_sms_" + phone;
        boolean flag = getRedisUtilsService().setIncrSecond(sendFlag, 60);
        if (!flag) {
            return GetRest.getFail("您已发送验证码,请1分钟后重试");
        }
        RestResponse result = SmsUtils.getInstance().sendSms(phone, validCode);
        if (result.isStatus()) {
            //保存验证码到缓存
            String key = "send_sms_" + phone;
            getRedisUtilsService().setKey(key, validCode, 600);
        }
        return result;
    }

    public RestResponse sendSmsAliyun(String phone, String templateCode) {
        String validPhone = RpxUtils.getInstance().validPhone(phone);
        if (!StringUtils.isBlank(validPhone)) {
            return GetRest.getFail(validPhone);
        }
        String validCode = RandNumUtils.getInstance().get(RandNumType.NUMBER, 6);
        //防止重复点击发送验证码
        String sendFlag = "sendflag_sms_" + phone;
        boolean flag = getRedisUtilsService().setIncrSecond(sendFlag, 60);
        if (!flag) {
            return GetRest.getFail("您已发送验证码,请1分钟后重试");
        }
        RestResponse result = SmsUtils.getInstance().aliyunSendSms(phone, validCode, templateCode);
        if (result.isStatus()) {
            //保存验证码到缓存
            String key = "send_sms_" + phone;
            getRedisUtilsService().setKey(key, validCode, 600);
        }
        return result;
    }

    /**
     * 校验验证码
     *
     * @param phone
     * @param validCode
     * @return
     */
    public RestResponse validSms(String phone, String validCode) {
        String validPhone = RpxUtils.getInstance().validPhone(phone);
        if (!StringUtils.isBlank(validPhone)) {
            return GetRest.getFail(validPhone);
        }
        if (StringUtils.isEmpty(validCode)) {
            return GetRest.getFail("请填写验证码");
        }
        //保存验证码到缓存
        String key = "send_sms_" + phone;
        String val = getRedisUtilsService().getKey(key);
        if (StringUtils.isEmpty(val)) {
            return GetRest.getFail("验证码过期或已使用");
        }
        if (!val.equals(validCode)) {
            return GetRest.getFail("验证码不正确");
        }
        //校验成功后删除
        getRedisUtilsService().deleteKey(key);
        return GetRest.getSuccess("验证通过");
    }

    /**
     * 注入redis
     *
     * @return
     */
    private RedisUtilsService getRedisUtilsService() {
        return SpringBeanFactoryUtils.getApplicationContext().getBean(RedisUtilsService.class);
    }
}
